//
//  HJScreeningViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJScreeningViewController.h"
#import "HJScreenView.h"
#import "HJScreenCell.h"
#import "NMRangeSlider.h"

@interface HJScreeningViewController ()
@property (weak, nonatomic) IBOutlet UIView *swiView;
@property(strong,nonatomic) LQXSwitch *swi;
@property(strong,nonatomic) HJScreenView *screenView;
@property(nonatomic,strong) NSMutableArray * dataArr;
@property(nonatomic,assign) BOOL cellHide;
@property(nonatomic,assign) NSIndexPath * cellRow;
@property(nonatomic,strong) NSNumber *years;
@property(nonatomic,assign) NSInteger min_price;
@property(nonatomic,assign) NSInteger max_price;
@property(nonatomic,assign) NSInteger have_goods;
@property(nonatomic,strong) NSNumber *right_btn;
@end

@implementation HJScreeningViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:HJColorRGB(255)] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:HJColorRGB(255)]];
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftNavImageName = @"close";
    self.rightNavTitle = @"重置";
    [self.tableView setBackgroundColor:[UIColor whiteColor]];
    [self configBottom];
    self.dataArr = [NSMutableArray array];
    _cellHide = NO;
    self.have_goods = 0;
    self.max_price = 5000;
    [self loadNetworkData];
    NSNumber * years = [[NSUserDefaults standardUserDefaults] objectForKey:@"years"];
    self.cellRow = [NSIndexPath indexPathForRow:[years integerValue] ? [years integerValue] : 0 inSection:0];
}
-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    CGFloat H;
    if (mScreenHeight<812) {
        H = mScreenHeight -60 -64;
    }else {
        H = mScreenHeight-60-TabbarHeight-X_Bottom_Space;
    }
    self.tableView.frame = CGRectMake(0, 0, mScreenWidth, H);
    self.tableView.tableHeaderView = [self configScreenView];
    self.tableView.tableFooterView = [self configBtmView];
}

-(void)loadNetworkData {
    [TZHttpTool post:YeasAPI params:nil success:^(NSDictionary *result) {
        [self.dataArr removeAllObjects];
        self.dataArr = result[@"data"];
        if (self.dataArr) {
            self.years = self.dataArr[0];
        }
        [self.tableView reloadData];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}

-(void)didClickLeftNavAction {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void)didClickRightNavAction {

    [[NSUserDefaults standardUserDefaults] setObject:@(0) forKey:@"row"];
    [[NSUserDefaults standardUserDefaults] setObject:@(0.0) forKey:@"min"];
    [[NSUserDefaults standardUserDefaults] setObject:@(1.0) forKey:@"max"];
    [self.screenView.slider setLowerValue:0.0 upperValue:1.0 animated:YES];
    
    self.screenView.priceNo.text = @"￥0~￥5000+";
    [[NSUserDefaults standardUserDefaults] setObject:@"￥0~￥5000+" forKey:@"priceNotext"];
    
    [[NSUserDefaults standardUserDefaults] setObject:@(0) forKey:@"have_goods"];
    [self.swi setOn:NO animated:YES];
    
    [[NSUserDefaults standardUserDefaults] setObject:@(0) forKey:@"years"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    [self.tableView reloadData];
    
    self.right_btn = @1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _cellHide ? self.dataArr.count : 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJScreenCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJScreenCell"];
    if (!cell) {
        NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"HJScreenCell" owner:nil options:nil];
        cell = [objs lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == self.cellRow.row) {
        cell.btn.selected = YES;
    }
    if (self.dataArr.count>0) {
       cell.yearLabel.text = [NSString stringWithFormat:@"%@",self.dataArr[indexPath.row]];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJScreenCell * cell =  (HJScreenCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (cell.cellSelecte) {
        return;
    }else {
        cell.cellSelecte = YES;
        cell.btn.selected = YES;
        self.years = self.dataArr[indexPath.row];
        [[NSUserDefaults standardUserDefaults] setObject:@(indexPath.row) forKey:@"years"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        self.right_btn = @0;
        if (self.cellRow) {
            HJScreenCell * cell1 =  (HJScreenCell *)[tableView cellForRowAtIndexPath:self.cellRow];
            cell1.cellSelecte = NO;
            cell1.btn.selected = NO;
        }
    }
    self.cellRow = indexPath;
}

-(UIView *)configScreenView {
    _screenView = [[HJScreenView alloc]init];
    _screenView.frame = CGRectMake(0, 0, mScreenWidth, 200);
    WeakSelf
    _screenView.maxAndminBlock = ^(float max, float min) {
       
        weakSelf.max_price = max * 5000;
        weakSelf.min_price = min * 5000;
        [[NSUserDefaults standardUserDefaults] setObject:@(min) forKey:@"min"];
        [[NSUserDefaults standardUserDefaults] setObject:@(max) forKey:@"max"];
        [[NSUserDefaults standardUserDefaults] setObject:weakSelf.screenView.priceNo.text forKey:@"priceNotext"];
        [[NSUserDefaults standardUserDefaults] synchronize];
         weakSelf.right_btn = @0;
    };
    return _screenView;
}

-(UIView *)configBtmView {
    UIView * view = [[NSBundle mainBundle] loadNibNamed:@"HJScreenBtmView" owner:self options:nil].firstObject;
    view.frame = CGRectMake(0, 0, mScreenWidth, 120);
    
    self.swi = [[LQXSwitch alloc]initWithFrame:CGRectMake(0, 0, 42, 28) onColor:HJImage(@"open") offColor:HJImage(@"swi") font:nil ballSize:28];
    NSNumber * setOn = [[NSUserDefaults standardUserDefaults]objectForKey:@"have_goods"];
    [self.swi setOn:setOn.boolValue animated:YES];
    [self.swi addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [self.swiView addSubview:self.swi];
    
    return view;
}

-(void)switchAction:(LQXSwitch *)swi {
    
    if (swi.on) {
        self.have_goods = 1;
        [[NSUserDefaults standardUserDefaults] setObject:@(1) forKey:@"have_goods"];
    }else {
        [[NSUserDefaults standardUserDefaults] setObject:@(0) forKey:@"have_goods"];
        self.have_goods = 0;
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
    self.right_btn = @0;
}
- (IBAction)moreBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    _cellHide = !_cellHide;
    [self.tableView reloadData];
}

-(void)configBottom {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat H;
    if (mScreenHeight<812) {
        H = mScreenHeight -60 -64;
    }else {
        H = mScreenHeight-60-TabbarHeight-X_Bottom_Space;
    }
    btn.frame = CGRectMake(0, H, mScreenWidth, 60);
    btn.layer.shadowOpacity = 0.2;
    btn.layer.shadowRadius = 3;
    btn.layer.shadowOffset = CGSizeZero;
    btn.layer.shadowColor = [UIColor blackColor].CGColor;
    [btn setTitle:@"筛选" forState:UIControlStateNormal];
    [btn setTitleColor:HJColorRGB(0) forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [btn setBackgroundColor:HJColorRGB(255)];
    [btn addTarget:self action:@selector(screenBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [self.view bringSubviewToFront:btn];
}

-(void)screenBtn:(UIButton*)btn {
    
    if (self.block) {
        self.block(self.have_goods, self.max_price, self.min_price, self.years, self.right_btn);
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    });
}

@end
